import { request } from '@umijs/max';

// 图表列表项类型
export interface ChartListItem {
  id: string;
  title: string;
  description?: string;
}

// 图表数据参数类型
export interface ChartDataParams {
  store: number;
  chartId: string;
  timeRangeStart: string;
  timeRangeEnd: string;
  limit?: number;
}

// 图表数据响应类型
export interface ChartDataResponse {
  title: string;
  subtitle?: string;
  xField: string;
  yField: string;
  data: Array<Record<string, any>>;
  meta: Record<string, {
    alias?: string;
    unit?: string;
  }>;
}

// 统计数据响应类型
export interface SummaryDataResponse {
  summary: {
    gmv: number;
    rate: number;
    same_rate: number;
    unit: string;
    meta: Record<string, {
      alias?: string;
      unit?: string;
    }>;
  };
}

// API响应基础类型
export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
}

export const dataService = {
  // 获取图表列表
  async getChartList(): Promise<ApiResponse<ChartListItem[]>> {
    return request('/admin/data/charts', {
      method: 'GET',
    });
  },

  // 获取图表数据
  async getChartData(params: ChartDataParams): Promise<ApiResponse<ChartDataResponse>> {
    return request('/admin/data/chart', {
      method: 'POST',
      data: params,
    });
  },

  // 获取统计数据
  async getSummaryData(params: ChartDataParams): Promise<ApiResponse<SummaryDataResponse>> {
    return request('/admin/data/summary', {
      method: 'POST',
      data: params,
    });
  }
}; 